<?php
namespace app\admin\controller;
use app\common\controller\Admin;
use think\Db;

Class Common extends Admin {

    protected function _initialize(){
        parent::_initialize();
        //栏目渲染
//      {"infomation":{"cate":7,"content":5,"test":1,"test1":5,"test2":5},"home":{"tool":7}}
        $destop ='';$module_name = '';$top_nav = [];$left_nav = [];
        if(isset($this->session['admin'])&&$this->controller!='Member'){
            $admin = $this->session['admin'];
            $privilege =  $admin['admin_privilege'];
            $top_nav_module = [];$left_nav_pri = [];
            if($privilege=='root'){
                $top_nav = Db::name('top_nav')->where('is_delete',0)
                    ->where('is_hide',0)->order('sort desc,id')->select();
                $left_nav = Db::name('left_nav')->where('module',strtolower($this->controller))
                    ->where('is_delete',0)->where('is_hide',0)
                    ->order('sort desc,id')->select();
            }
            else{
                $privilege = decode( $admin['admin_privilege'] );
                foreach ($privilege as $k=>$v){
                    array_push($top_nav_module,$k);
                }
                $top_nav = Db::name('top_nav')->where('is_delete',0)
                    ->where('is_hide',0)->whereIn('module',$top_nav_module)
                    ->order('sort desc,id')->select();
                foreach ($privilege[strtolower($this->controller)] as $k=>$v){
                    array_push($left_nav_pri,$k);
                }
                $left_nav = Db::name('left_nav')->where('module',strtolower($this->controller))
                    ->whereIn('controller',$left_nav_pri)->where('is_delete',0)->where('is_hide',0)
                    ->order('sort desc,id')->select();
            }
            foreach ($left_nav as $k=>$v){
                if($v['is_external']==0){
                    $left_nav[$k]['url']= $this->app . $v['url'];
                }
            }
            $left_nav = getTree($left_nav);

            foreach ($top_nav as $k=>$v){
                $top_nav[$k]['url'] = $this->app . '/admin/' . $v['module'] . '/index';
                if($v['module']==strtolower($this->controller)){
                    $module_name = $v['name'];
                }
            }
            $destop = $this->app .'/'. strtolower($this->controller) .'/home/index';
        }

        $this->assign([
            'destop'=>$destop,
            'top_nav'=>$top_nav,
            'left_nav'=>$left_nav,
            'module_name'=>$module_name
        ]);
    }


}

